#include "ShopTreeModel.h"

#include <QtGui>

#include "InvoiceItem.h"
#include "InvoicePrinter.h"

using namespace ecocity;

ShopTreeModel::ShopTreeModel( const ecocity::Shop& shop,
                              QObject *parent )
    : QStandardItemModel( parent ),
      m_shop( shop )
{
    QStringList headerLabels;
    headerLabels << "Cliente";
    headerLabels << "Facturado";
    headerLabels << "Beneficio";

    QStandardItem* cliente = new QStandardItem( "Cliente" );
    QStandardItem* facturado = new QStandardItem( "Facturado" );
    QStandardItem* beneficio = new QStandardItem( "Beneficio" );

    cliente->setData( Qt::AlignCenter, Qt::TextAlignmentRole );
    facturado->setData( Qt::AlignCenter, Qt::TextAlignmentRole );
    beneficio->setData( Qt::AlignCenter, Qt::TextAlignmentRole );

    setHorizontalHeaderItem( CUSTOMER_NAME, cliente );
    setHorizontalHeaderItem( ITEM_TOTAL, facturado );
    setHorizontalHeaderItem( INVOICE_PROFIT, beneficio );

    // Not used since I want them Qt::AlignCenter
    //setHorizontalHeaderLabels(  headerLabels );

    reset( shop );
}

//---------------------------------------------------------

Qt::ItemFlags ShopTreeModel::flags(const QModelIndex &index) const
{
    if ( !index.isValid() )
        return 0;

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

//---------------------------------------------------------

void
ShopTreeModel::reset( const Shop& shop )
{
    removeRows( 0, rowCount( ) );

    const std::vector< Customer > customers = shop.customers( );

    for( size_t customer_row = 0; customer_row < customers.size(); ++customer_row )
    {
        Customer customer = customers.at( customer_row );

        const std::vector< Invoice >& invoices = shop.invoices( customer );

        if ( invoices.empty( ) )
        {
            continue;
        }

        QStandardItem* item_customer_name =
                new QStandardItem( customer.name( ) );

        QStandardItem* item_sum_invoices =
                new QStandardItem( );

        QStandardItem* item_sum_profit =
                new QStandardItem( );

        float sum_invoices = 0;
        float sum_profit = 0;

        for( size_t invoice_row = 0; invoice_row < invoices.size(); ++invoice_row )
        {
            const Invoice& invoice = invoices.at( invoice_row );
            sum_invoices += invoice.total_cost();
            sum_profit += invoice.profit();

            QStandardItem* item_date = new QStandardItem( );
            QStandardItem* item_invoice_total = new QStandardItem( );
            QStandardItem* item_invoice_profit = new QStandardItem( );

            item_date->setData( invoice.date( ), Qt::EditRole );

            item_invoice_total->setData( invoice.total_cost(), Qt::EditRole );
            item_invoice_total->setData( Qt::AlignRight, Qt::TextAlignmentRole );

            item_invoice_profit->setData( invoice.profit( ), Qt::EditRole );
            item_invoice_profit->setData( Qt::AlignRight, Qt::TextAlignmentRole );

            const std::vector< InvoiceItem > items = invoice.items( );

            for( size_t item_row = 0; item_row < items.size(); ++item_row )
            {
                const InvoiceItem& item = items.at( item_row );

                QString description;
                QTextStream stream( &description );

                InvoicePrinter::print_invoice_item_description( stream, item );

                QStandardItem* item_description =
                        new QStandardItem( description );

                QStandardItem* item_price =
                        new QStandardItem( QString::number( item.total_cost( ), 'f', 2 ) );

                item_price->setData( Qt::AlignRight, Qt::TextAlignmentRole );

                item_date->setChild( item_row, ITEM_DESCRIPTION, item_description );
                item_date->setChild( item_row, ITEM_TOTAL, item_price );
            }

            item_customer_name->setChild( invoice_row, INVOICE_DATE, item_date );
            item_customer_name->setChild( invoice_row, INVOICE_TOTAL, item_invoice_total );
            item_customer_name->setChild( invoice_row, INVOICE_PROFIT, item_invoice_profit );

            item_sum_invoices->setData( sum_invoices, Qt::EditRole );
            item_sum_profit->setData( sum_profit, Qt::EditRole );
        }

        QList<QStandardItem*> customer_row_items;

        customer_row_items << item_customer_name
                           << item_sum_invoices
                           << item_sum_profit;

        appendRow( customer_row_items );
    }

    m_shop = shop;
}


//---------------------------------------------------------

const Invoice*
ShopTreeModel::invoice( const QModelIndex &index ) const
{
    if ( !index.isValid()
         || index.row() != 0 )
    {
        return 0;
    }

    QModelIndex index_parent = index.parent( );
    QString customer_name = index_parent.data( ).toString( );
    QDate date = index.data( ).toDate( );
    float invoice_total = data(
                this->index( index.row(), INVOICE_TOTAL, index_parent ) ).toFloat( );

    float invoice_profit = data(
                this->index( index.row(), INVOICE_PROFIT, index_parent ) ).toFloat( );

    const std::vector< Invoice >& invoices = m_shop.invoices( );

    for ( size_t i = 0; i < invoices.size(); ++i )
    {
        const Invoice& invoice = invoices[ i ];

        if ( invoice.customer().name() == customer_name
             && invoice.date() == date
             && invoice.total_cost() == invoice_total
             && invoice.profit() == invoice_profit )
        {
            return &invoice;
        }
    }

    return 0;
}
